package com.yanggu.mapreduce.customizeInputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/6 12:57
 * @description:
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {

    private FileSplit split;

    private Configuration configuration;

    private Text key = new Text();

    private BytesWritable value = new BytesWritable();

    private Boolean flag = true;


    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        //初始化方法
        this.split = (FileSplit) split;
        this.configuration = context.getConfiguration();
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {

        //核心业务逻辑
        if (this.flag) {
            //1.根据切片信息获取path对象
            Path path = split.getPath();

            //2.根据path对象和conf对象获取fs对象
            FileSystem fileSystem = path.getFileSystem(configuration);

            //3.获得输入流
            FSDataInputStream inputStream = fileSystem.open(path);

            //4.流的拷贝
            byte[] buf = new byte[(int) split.getLength()];
            IOUtils.readFully(inputStream, buf, 0, buf.length);

            //5.设置value
            value.set(buf, 0, buf.length);

            //6.设置key
            key.set(path.toString());

            //7.修改标记为false
            flag = false;

            //8. 关闭资源
            IOUtils.closeStream(inputStream);

            return true;
        }
        return false;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return key;
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }

}
